/**
 * Diplay contact informations an allows editing
 *
 * @author Martin LEGRIS
 */
package View;

import Model.Contact;
import adressbook.AdressBook;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class ViewContact extends JFrame implements ActionListener {

    private final JPanel container = new JPanel();
    private final JPanel buttons_pan = new JPanel();
    private final JPanel form = new JPanel();
    private final JTextField fname = new JTextField();
    private final JTextField lname = new JTextField();
    private final JTextField haddress = new JTextField();
    private final JTextField caddress = new JTextField();
    private final JTextField mphone = new JTextField();
    private final JTextField hphone = new JTextField();
    private final JTextField prof_mobile_phone = new JTextField();
    private final JTextField prof_office_phone = new JTextField();
    BufferedImage img = null;
    JLabel imgLabel = new JLabel();
    //private final JTextField home_page = new JTextField();
    JEditorPane home_page = new JEditorPane();
    //private final JTextField prof_email = new JTextField();
    JEditorPane prof_email = new JEditorPane();
    //private final JTextField perso_email = new JTextField();
    JEditorPane perso_email = new JEditorPane();
    private final JTextField skype = new JTextField();
    private final JTextField notes = new JTextField();
    private final JButton editContact = new JButton();
    private final JButton save = new JButton();
    private final JButton cancel = new JButton();
    private int contactIndex = -1;
    String data[] = new String[13];

    public ViewContact(Contact myContact, int index) throws IOException {

        contactIndex = index;

        //Create and set up the window.
        this.setTitle("Contact");
        this.setSize(300, 200);
        this.setLocationRelativeTo(null); //on positionne la fenetre au centre
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setContentPane(container);

        //JPanel buttons
        buttons_pan.setLayout(new GridLayout(4, 1));

        Dimension minSize = new Dimension();
        Dimension maxSize = new Dimension();
        minSize.setSize(100, 400);
        maxSize.setSize(150, 500);

        buttons_pan.setMinimumSize(minSize);
        buttons_pan.setMaximumSize(maxSize);
        container.add(buttons_pan);

        //JPanel form
        GridLayout layout = new GridLayout(13, 2);
        layout.setHgap(5);
        layout.setVgap(5);
        form.setLayout(layout);
        container.add(form);

        //Picture
        try {
            img = ImageIO.read(new File(myContact.getPicture()));
        } catch (IOException e) {
            img = ImageIO.read(new File("avatar-vide.jpeg"));

        }
        imgLabel.setIcon(new ImageIcon(img));
        buttons_pan.add(imgLabel);

        //JButton
        editContact.setText("Edit");
        buttons_pan.add(editContact);
        editContact.addActionListener(this);

        save.setText("Save");
        save.setVisible(false);
        buttons_pan.add(save);
        save.addActionListener(this);

        cancel.setText("Cancel");
        buttons_pan.add(cancel);
        cancel.addActionListener(this);

        //form
        JLabel lfname = new JLabel(AdressBook.FIRSTNAME);
        form.add(lfname);
        fname.setText(myContact.getFname());
        fname.setEditable(false);
        form.add(fname);

        JLabel llname = new JLabel(AdressBook.LASTNAME);
        lname.setText(myContact.getLname());
        lname.setEditable(false);
        form.add(llname);
        form.add(lname);

        JLabel lhaddress = new JLabel(AdressBook.HOME_ADDRRESS);
        haddress.setText(myContact.getHome_address());
        haddress.setEditable(false);
        form.add(lhaddress);
        form.add(haddress);

        JLabel lcaddress = new JLabel(AdressBook.COMPANY_ADDRESS);
        caddress.setText(myContact.getCompany_address());
        caddress.setEditable(false);
        form.add(lcaddress);
        form.add(caddress);

        JLabel lmphone = new JLabel(AdressBook.PERSO_MOBILE_PHONE);
        mphone.setText(myContact.getMobile_phone());
        mphone.setEditable(false);
        form.add(lmphone);
        form.add(mphone);

        JLabel lhphone = new JLabel(AdressBook.HOME_PHONE);
        hphone.setText(myContact.getHome_phone());
        hphone.setEditable(false);
        form.add(lhphone);
        form.add(hphone);

        JLabel lpmobile = new JLabel(AdressBook.PERSO_MOBILE_PHONE);
        prof_mobile_phone.setText(myContact.getProf_mobile_phone());
        prof_mobile_phone.setEditable(false);
        form.add(lpmobile);
        form.add(prof_mobile_phone);

        JLabel lpoffice = new JLabel(AdressBook.PROF_OFFICE_PHONE);
        prof_office_phone.setText(myContact.getProf_office_phone());
        prof_office_phone.setEditable(false);
        form.add(lpoffice);
        form.add(prof_office_phone);

        JLabel lhome_page = new JLabel(AdressBook.HOME_PAGE);
        String message = "<html><a href='http://" + myContact.getHome_page() + "'>" + myContact.getHome_page() + "</a> </html>";
        home_page = new JEditorPane("text/html", message);
        home_page.setEditable(false);
        home_page.setBackground(null);

        home_page.addHyperlinkListener(new HyperlinkListener() {
            @Override
            public void hyperlinkUpdate(HyperlinkEvent e) {
                if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {

                    try {

                        Desktop.getDesktop().browse(e.getURL().toURI());
                        //Desktop.getDesktop().browse(new URL(myContact.getHome_page()).toURI());
                    } catch (URISyntaxException ex) {
                        Logger.getLogger(ViewContact.class.getName()).log(Level.SEVERE, null, ex);

                    } catch (IOException e1) {
                        throw new RuntimeException(e1);
                    }
                }
            }
        });
        form.add(lhome_page);
        form.add(home_page);

        JLabel lpemail = new JLabel(AdressBook.PROF_EMAIL);
        message = "<html><a href='mailto:" + myContact.getProf_email() + "'>" + myContact.getProf_email() + "</a> </html>";
        prof_email = new JEditorPane("text/html", message);
        prof_email.setEditable(false);
        prof_email.setBackground(null);
        prof_email.addHyperlinkListener(new HyperlinkListener() {
            @Override
            public void hyperlinkUpdate(HyperlinkEvent e) {
                if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
                    try {
                        Desktop.getDesktop().mail(e.getURL().toURI());
                    } catch (IOException e1) {
                        throw new RuntimeException(e1);
                    } catch (URISyntaxException e1) {
                        throw new RuntimeException(e1);
                    }
                }
            }
        });
        form.add(lpemail);
        form.add(prof_email);

        JLabel lperso_email = new JLabel(AdressBook.PERSO_EMAIL);
        message = "<html><a href='mailto:" + myContact.getPerso_email() + "'>" + myContact.getPerso_email() + "</a> </html>";
        perso_email = new JEditorPane("text/html", message);
        perso_email.setEditable(false);
        perso_email.setBackground(null);
        perso_email.addHyperlinkListener(new HyperlinkListener() {
            @Override
            public void hyperlinkUpdate(HyperlinkEvent e) {
                if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
                    try {
                        Desktop.getDesktop().mail(e.getURL().toURI());
                    } catch (IOException e1) {
                        throw new RuntimeException(e1);
                    } catch (URISyntaxException e1) {
                        throw new RuntimeException(e1);
                    }
                }
            }
        });

        form.add(lperso_email);
        form.add(perso_email);

        JLabel lskype = new JLabel(AdressBook.SKYPE);

        skype.setText(myContact.getSkype());
        skype.setEditable(
                false);
        form.add(lskype);

        form.add(skype);
        JLabel lnotes = new JLabel(AdressBook.NOTES);

        notes.setText(myContact.getNotes());
        notes.setEditable(
                false);
        form.add(lnotes);

        form.add(notes);

        //Display the window.
        this.pack();

        this.setVisible(
                true);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        //Lorsque l'on clique sur le bouton, on met à jour le JLabel
        if (arg0.getSource() == editContact) {
            fname.setEditable(true);
            lname.setEditable(true);
            haddress.setEditable(true);
            caddress.setEditable(true);
            mphone.setEditable(true);
            hphone.setEditable(true);
            prof_mobile_phone.setEditable(true);
            prof_office_phone.setEditable(true);
            home_page.setEditable(true);
            prof_email.setEditable(true);
            perso_email.setEditable(true);
            skype.setEditable(true);
            notes.setEditable(true);

            editContact.setVisible(false);
            save.setVisible(true);
        }

        if (arg0.getSource() == save) {
            data[0] = fname.getText();
            data[1] = lname.getText();
            data[2] = haddress.getText();
            data[3] = caddress.getText();
            data[4] = mphone.getText();
            data[5] = hphone.getText();
            data[6] = prof_mobile_phone.getText();
            data[7] = prof_office_phone.getText();
            data[8] = home_page.getText();
            data[9] = prof_email.getText();
            data[10] = perso_email.getText();
            data[11] = skype.getText();
            data[12] = notes.getText();
            AdressBook.editContact(data, contactIndex);

            this.setVisible(false);
        }
        if (arg0.getSource() == cancel) {
            this.setVisible(false);
        }

        if (arg0.getSource() == home_page) {
            System.out.println("home_page");
            try {
                Desktop.getDesktop().browse(new URL("http://www.google.com").toURI());
            } catch (IOException ex) {
                Logger.getLogger(ViewContact.class.getName()).log(Level.SEVERE, null, ex);
            } catch (URISyntaxException ex) {
                Logger.getLogger(ViewContact.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void hyperlinkUpdate(final HyperlinkEvent e) {
        if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
            try {
                Desktop.getDesktop().mail(e.getURL().toURI());
            } catch (IOException e1) {
                throw new RuntimeException(e1);
            } catch (URISyntaxException e1) {
                throw new RuntimeException(e1);
            }
        }
    }
}
